home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-05-22 | 6.9 KB | 281 lines | [TEXT/CWIE] |
- //
- // File: Texture.c
- //
- // Contains: Support for adding a QuickTime movie or a PICT as a texture on a QD3D object.
- //
- // Written by: Tim Monroe
- // Based (heavily!) on BoxMoov code by Rick Evans and Robert Dierkes
- //
- // Copyright: © 1996 by Apple Computer, Inc., all rights reserved.
- //
- // Change History (most recent first):
- //
- // <3> 01/14/97 rtm added support for PICT textures
- // <2> 12/17/96 rtm modified VR3DTexture_AddToGroup to replace a texture shader
- // <1> 12/16/96 rtm first file; revised to personal coding style
- //
- //
- // TODO:
-
-
- // header files
-
- #include <QuickDraw.h>
- #include <Resources.h>
-
- #include "QD3D.h"
- #include "QD3DGroup.h"
- #include "QD3DShader.h"
- #include "QD3DSet.h"
-
- #include "Texture.h"
- #include "TestFunctions.h"
-
-
- //////////
- //
- // VR3DTexture_New
- // Create a new texture from a QuickTime movie or PICT file.
- //
- //////////
-
- TextureHdl VR3DTexture_New (Boolean isTextureMovie)
- {
- unsigned long myPictMapAddr;
- GWorldPtr myGWorld;
- PixMapHandle myPixMap;
- unsigned long myPictRowBytes;
- QDErr myErr;
- GDHandle myOldGD;
- GWorldPtr myOldGW;
- Boolean mySuccess;
- Rect myBounds;
- TQ3StoragePixmap *myStrgPMapPtr;
- TextureHdl myTexture = NULL;
-
- myTexture = (TextureHdl)NewHandleClear(sizeof(Texture));
- if (myTexture == NULL)
- return(myTexture);
-
- HLock((Handle)myTexture);
-
- // save current port
- GetGWorld(&myOldGW, &myOldGD);
-
- if (isTextureMovie) {
- // prompt the user to select a movie;
- // open the movie and store its address in our data structure
- mySuccess = VR3DObjects_GetEmbeddedMovie(&(**myTexture).fMovie);
- if (!mySuccess)
- goto bail;
-
- // get the size of the movie
- GetMovieBox((**myTexture).fMovie, &myBounds);
-
- // create a new offscreen graphics world (into which we will draw the movie)
- myErr = NewGWorld(&myGWorld, 32, &myBounds, 0, 0, useTempMem);
- if (myErr != noErr) {
- StopMovie((**myTexture).fMovie);
- DisposeMovie((**myTexture).fMovie);
- (**myTexture).fMovie = NULL;
- mySuccess = false;
- goto bail;
- }
- } else {
- // prompt the user to select a PICT;
- // open the PICT and store its address in our data structure
- (**myTexture).fPicture = VR3DObjects_GetEmbeddedPicture();
- if ((**myTexture).fPicture == NULL) {
- mySuccess = false;
- goto bail;
- }
-
- // get the size of the picture
- myBounds = (**(**myTexture).fPicture).picFrame;
-
- // create a new offscreen graphics world (into which we will draw the picture)
- myErr = NewGWorld(&myGWorld, 32, &myBounds, 0, 0, useTempMem);
- if (myErr != noErr) {
- KillPicture((**myTexture).fPicture);
- (**myTexture).fPicture = NULL;
- mySuccess = false;
- goto bail;
- }
- }
-
- mySuccess = true;
-
- myPixMap = GetGWorldPixMap(myGWorld);
- LockPixels(myPixMap);
- myPictMapAddr = (unsigned long)GetPixBaseAddr(myPixMap);
- myPictRowBytes = (unsigned long)(**myPixMap).rowBytes & 0x3fff;
-
- SetGWorld(myGWorld, NULL);
-
- // create a storage object associated with the new offscreen graphics world
- myStrgPMapPtr = &(**myTexture).fStoragePixmap;
- myStrgPMapPtr->image = Q3MemoryStorage_NewBuffer((void *)myPictMapAddr,
- myPictRowBytes * myBounds.bottom,
- myPictRowBytes * myBounds.bottom);
- myStrgPMapPtr->width = myBounds.right;
- myStrgPMapPtr->height = myBounds.bottom;
- myStrgPMapPtr->rowBytes = myPictRowBytes;
- myStrgPMapPtr->pixelSize = 32;
- myStrgPMapPtr->pixelType = kQ3PixelTypeRGB32;
- myStrgPMapPtr->bitOrder = kQ3EndianBig;
- myStrgPMapPtr->byteOrder = kQ3EndianBig;
-
- if (isTextureMovie) {
- // start playing the movie in a loop
- VR3DObjects_LoopEmbeddedMovie((**myTexture).fMovie, myGWorld);
- } else {
- // draw the picture into the offscreen graphics world
- DrawPicture((**myTexture).fPicture, &myBounds);
- }
-
- (**myTexture).fpGWorld = myGWorld;
-
- bail:
- SetGWorld(myOldGW, myOldGD);
-
- HUnlock((Handle)myTexture);
-
- if (mySuccess)
- return(myTexture);
- else
- return(NULL);
- }
-
-
- //////////
- //
- // VR3DTexture_AddToGroup
- // Create a new texture shader based on the pixmap storage data and add it to the group.
- //
- //////////
-
- TQ3Status VR3DTexture_AddToGroup (TextureHdl theTexture, TQ3GroupObject theGroup)
- {
- TQ3Status myStatus = kQ3Failure;
- TQ3StoragePixmap myStoragePixmap;
- TQ3TextureObject myTextureObject;
-
- if (theTexture == NULL || theGroup == NULL)
- return(myStatus);
-
- myStoragePixmap = (**theTexture).fStoragePixmap;
- myTextureObject = Q3PixmapTexture_New(&myStoragePixmap);
- if (myTextureObject != NULL) {
-
- TQ3ShaderObject myTextureShader;
-
- myTextureShader = Q3TextureShader_New(myTextureObject);
- Q3Object_Dispose(myTextureObject);
-
- if (myTextureShader != NULL) {
- TQ3GroupPosition myPosition = NULL;
-
- // look for an existing texture shader in group
- Q3Group_GetFirstPositionOfType(theGroup, kQ3SurfaceShaderTypeTexture, &myPosition);
- if (myPosition != NULL) {
-
- // there is an existing texture shader; just replace it
- Q3Group_SetPositionObject(theGroup, myPosition, myTextureShader);
- } else {
-
- // there is no existing texture shader; add one to the group
- Q3Group_GetFirstPosition(theGroup, &myPosition);
- Q3Group_AddObjectBefore(theGroup, myPosition, myTextureShader);
- }
-
- Q3Object_Dispose(myTextureShader);
- myStatus = kQ3Success;
- }
- }
-
- return(myStatus);
- }
-
-
- //////////
- //
- // VR3DTexture_Delete
- // Deallocate the texture.
- //
- //////////
-
- Boolean VR3DTexture_Delete (TextureHdl theTexture)
- {
- if (theTexture == NULL)
- return(false);
-
- if ((**theTexture).fpGWorld == NULL)
- return(false);
-
- HLock((Handle)theTexture);
-
- if ((**theTexture).fMovie != NULL) {
- StopMovie((**theTexture).fMovie);
- DisposeMovie((**theTexture).fMovie);
- (**theTexture).fMovie = NULL;
- }
-
- if ((**theTexture).fPicture != NULL) {
- KillPicture((**theTexture).fPicture);
- (**theTexture).fPicture = NULL;
- }
-
- DisposeGWorld((**theTexture).fpGWorld);
- (**theTexture).fpGWorld = NULL;
-
- if ((**theTexture).fStoragePixmap.image != NULL) {
- Q3Object_Dispose((**theTexture).fStoragePixmap.image);
- (**theTexture).fStoragePixmap.image = NULL;
- }
-
- HUnlock((Handle)theTexture);
- DisposeHandle((Handle)theTexture);
- return(true);
- }
-
-
- //////////
- //
- // VR3DTexture_NextFrame
- // Advance the texture's movie to the next frame.
- //
- //////////
-
- Boolean VR3DTexture_NextFrame (TextureHdl theTexture)
- {
- TQ3StoragePixmap *myStrgPMapPtr;
- PixMapPtr myPixMap;
- long mySize;
- TQ3Status myStatus;
-
- // if fpGWorld is non-NULL then the fMovie is non-NULL and the movie needs updating
- if ((**theTexture).fpGWorld == NULL)
- return(false);
-
- HLock((Handle)theTexture);
-
- if ((**theTexture).fMovie) {
- MoviesTask((**theTexture).fMovie, 0); // draw the next movie frame
- }
-
- myStrgPMapPtr = &(**theTexture).fStoragePixmap;
-
- // tell QD3D the buffer changed
- myPixMap = *((**theTexture).fpGWorld)->portPixMap;
- mySize = myStrgPMapPtr->height * myStrgPMapPtr->rowBytes;
-
- myStatus = Q3MemoryStorage_SetBuffer(myStrgPMapPtr->image, (void *)myPixMap->baseAddr, mySize, mySize);
-
- HUnlock((Handle)theTexture);
-
- if (myStatus == kQ3Success)
- return(true);
- else
- return(false);
- }
-